/**
 * 
 */
package toutiao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 编程3 {

	static class Mission implements Comparable<Mission> {
		int pmid;
		int time;
		int needTime;
		int priority;
		int finishTime;

		@Override
		public int compareTo(Mission o2) {
			if (priority != o2.priority) {
				return priority - o2.priority;
			}
			if (needTime != o2.needTime) {
				return needTime - o2.needTime;
			}
			return time - o2.time;
		}

		@Override
		public String toString() {
			return time + " " + needTime + " " + priority;
		}
	}

	static class MissionOfOnePerson implements Comparable<MissionOfOnePerson> {
		int pmId;
		boolean needTODO = false;
		PriorityQueue<Mission> waitedMissons = new PriorityQueue<>();

		@Override
		public int compareTo(MissionOfOnePerson o2) {
			if (waitedMissons.peek().needTime != o2.waitedMissons.peek().needTime) {
				return waitedMissons.peek().needTime - o2.waitedMissons.peek().needTime;
			}
			return pmId - o2.pmId;
		}

		@Override
		public String toString() {
			return pmId + " " + waitedMissons.toString();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();
		int M = scanner.nextInt();
		int P = scanner.nextInt();
		PriorityQueue<MissionOfOnePerson> allPMMissions = new PriorityQueue<>();
		HashMap<Integer, MissionOfOnePerson> missonGroup = new HashMap<>();
		ArrayList<Mission> allMission = new ArrayList<>();
		int minTime = Integer.MAX_VALUE;// 最小开始时间
		for (int i = 0; i < P; i++) {
			Mission mission = new Mission();
			mission.pmid = scanner.nextInt();
			mission.time = scanner.nextInt();
			mission.priority = scanner.nextInt();
			mission.needTime = scanner.nextInt();
			minTime = Math.min(minTime, mission.time);
			if (missonGroup.get(mission.pmid) == null) {
				MissionOfOnePerson missions = new MissionOfOnePerson();
				missions.pmId = mission.pmid;
				missonGroup.put(mission.pmid, missions);
			} else {
			}
			allMission.add(mission);
		}
		ArrayList<Mission> missionSortByTime = new ArrayList<>(allMission);
		Collections.sort(missionSortByTime, new Comparator<Mission>() {

			@Override
			public int compare(Mission o1, Mission o2) {
				return o1.time - o2.time;
			}
		});
		PriorityQueue<Integer> peoples = new PriorityQueue<>(M);// 每个人的时间
		for (int i = 0; i < M; i++) {
			peoples.add(minTime);
		}
		int indexOfMission = 0;// 按时间排序的
		int finishIndex = 0;// 完成任务数
		while (finishIndex < P) {
			// 把start<=最近完成时间的可以激活
			int curTime = peoples.poll();
			while (indexOfMission < P && missionSortByTime.get(indexOfMission).time <= curTime) {
				Mission mission = missionSortByTime.get(indexOfMission);
				MissionOfOnePerson pMissionOfOnePerson = missonGroup.get(mission.pmid);
				pMissionOfOnePerson.waitedMissons.add(mission);
				if (!missonGroup.get(missionSortByTime.get(indexOfMission).pmid).needTODO) {
					pMissionOfOnePerson.needTODO = true;
					allPMMissions.add(pMissionOfOnePerson);
				}
				indexOfMission++;
			}
			// 从里面挑一个任务
			if (allPMMissions.isEmpty()) {// 没有可以执行的任务等待
				peoples.add(missionSortByTime.get(indexOfMission).time);
				continue;
			}
			MissionOfOnePerson missionOfOnePerson = allPMMissions.peek();
			Mission mission = missionOfOnePerson.waitedMissons.poll();
			if (missionOfOnePerson.waitedMissons.isEmpty()) {
				missionOfOnePerson.needTODO = false;
				allPMMissions.poll();
			}
			int nextTime = Math.max(curTime, mission.time) + mission.needTime;
			mission.finishTime = nextTime;
			peoples.add(nextTime);
			finishIndex++;
		}

		for (Mission mission : allMission) {
			System.out.println(mission.finishTime);
		}
	}
	// 2 2 5
	// 1 1 1 2
	// 1 2 1 1
	// 1 3 2 2
	// 2 1 1 2
	// 2 3 5 5
}
